package com.gunjan.controller;

import com.gunjan.FaceHelper;
import com.gunjan.model.Face2FaceRequest;
import com.sun.imageio.plugins.jpeg.JPEG;

import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Base64;

import face.entity.FaceModel;
import face.sdk.FaceAPI;
import org.springframework.http.*;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.io.*;




/**
 * Created by Gunjan Kumar on 4/10/2017.
 */
@RestController
public class ServiceController {
    private HttpHeaders responseHeaders = new HttpHeaders();


    @ResponseBody
    @RequestMapping(value="/face2face", method=RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    public String face2Face(@RequestBody Face2FaceRequest jsonParam)
    {
        String returnStr="";
        String img1=jsonParam.getFaceimage1();
        String img2=jsonParam.getFaceimage2();
        byte[] img1_buffer=null;
        byte[] img2_buffer=null;
        BufferedImage bufferedImage1 = null;
        BufferedImage bufferedImage2 = null;
        int maxFaceDetectedFacecount=1;
        if (!img1.isEmpty() && !img2.isEmpty())
        {
            try
            {
                img1_buffer = Base64.getDecoder().decode(img1);
            }
            catch (Exception e)
            {
                returnStr = "decode img1 error ";
            }
            try {
                img2_buffer=Base64.getDecoder().decode(img2);
            }catch (Exception e){
                returnStr = "decode img2 error ";
            }
            if (img1_buffer!=null&&img2_buffer!=null)
            {
                InputStream inputStream1=new ByteArrayInputStream(img1_buffer);
                InputStream inputStream2=new ByteArrayInputStream(img2_buffer);
                try {
                    bufferedImage1=ImageIO.read(inputStream1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    bufferedImage2=ImageIO.read(inputStream2);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (bufferedImage1!=null&&bufferedImage2!=null)
                {
                    //人脸检测
                    FaceModel[] faceModel1 = FaceHelper.getInstance().getFaceAPI().detectFacesModel(bufferedImage1,maxFaceDetectedFacecount);
                    FaceModel[] faceModel2=FaceHelper.getInstance().getFaceAPI().detectFacesModel(bufferedImage2,maxFaceDetectedFacecount);
                    if (faceModel1!=null&&faceModel2!=null)
                    {
                        //如果照片中有多个人脸，可分别提取特征值，计算相似度可以用
                        //对应人脸的特征值计算相似度即可
                        //本例子中分别提取照片中的第1个人脸进行比对；
                        byte[] feature1 = FaceHelper.getInstance().getFaceAPI().detectFaceFeature(bufferedImage1,
                                faceModel1[0]);
                        byte[] feature2=FaceHelper.getInstance().getFaceAPI().detectFaceFeature(bufferedImage2,
                                faceModel2[0]);
                        if (feature1!=null&&feature2!=null)
                        {
                            float scroe=FaceHelper.getInstance().getFaceAPI().feature2feature(feature1,
                                    feature2);

                            NumberFormat nt = NumberFormat.getPercentInstance();
                            //设置百分数精确度2即保留两位小数

                            returnStr="score="+nt.format(scroe);

                        }

                    }
                }
            }
        }
        else
        {
            returnStr = "img1 or img2 is empty.";
        }
        return returnStr;
    }

}
